﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace Sakura.Games.StgFramework.Builder.UI
{
	[ValueConversion(typeof(bool), typeof(Visibility))]
	public class BooleanToVisibilityConverter : IValueConverter
	{
		/// <summary>
		/// 获取或设置 <c>true</c> 对应的转换值。
		/// </summary>
		public Visibility TrueValue
		{
			get;
			set;
		}

		/// <summary>
		/// 获取或设置 <c>false</c> 对应的转换值。
		/// </summary>
		public Visibility FalseValue
		{
			get;
			set;
		}

		/// <summary>
		/// 将 <see cref="System.Boolean"/> 类型的值转换为 <see cref="System.Windows.Visibility"/> 类型的值。
		/// </summary>
		/// <param name="value">要转换的值。</param>
		/// <param name="targetType">转换的目标类型。</param>
		/// <param name="parameter">转换的参数。</param>
		/// <param name="culture">转换的区域信息。</param>
		/// <returns>转换后的结果。</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			switch ((bool)value)
			{
				case true:
					return this.TrueValue;
				case false:
					return this.FalseValue;
			}

			return null;
		}

		/// <summary>
		/// 将 <see cref="System.Windows.Visibility"/> 类型的值转换为 <see cref="System.Boolean"/> 类型的值。
		/// </summary>
		/// <param name="value">要转换的值。</param>
		/// <param name="targetType">转换的目标类型。</param>
		/// <param name="parameter">转换的参数。</param>
		/// <param name="culture">转换的区域信息。</param>
		/// <returns>转换后的结果。</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			// 如果值相等则返回不可转换的结果。
			if (this.TrueValue == this.FalseValue)
			{
				return null;
			}

			var v = (Visibility)value;

			if (v == this.TrueValue)
			{
				return true;
			}
			else if (v == this.FalseValue)
			{
				return false;
			}
			else
			{
				return null;
			}
		}
	}
}
